﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using NextCMS.Models;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class Sites : ISite
    {
        private static string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/Sites.xml";
        XDocument doc = null;
        public Sites()
        {
            doc = XDocument.Load(FilePath);  //load the xml file.
        }
        public IEnumerable<SiteModel> GetAll()
        {
            var q = from d in doc.Descendants("Site")
                    select new SiteModel
                    {
                        Id = (int)d.Element("Id"),
                        Name = d.Element("Name").Value,
                        Description = d.Element("Description").Value,
                        SystemDefined = (bool)d.Element("SystemDefined"),
                    };
            return q;
        }
        public SiteModel GetById(int Id)
        {

            var q = from d in doc.Descendants("Site")
                    where (int)d.Element("Id") == Id
                    select new SiteModel
                    {
                        Id = (int)d.Element("Id"),
                        Name = d.Element("Name").Value,
                        Description = d.Element("Description").Value,
                        SystemDefined = (bool)d.Element("SystemDefined"),
                    };

            return q.FirstOrDefault();
        }
        public SiteModel GetByName(string SiteName)
        {

            var q = from d in doc.Descendants("Site")
                    where d.Element("Name").Value.ToLower() == SiteName.ToLower()
                    select new SiteModel
                    {
                        Id = (int)d.Element("Id"),
                        Name = d.Element("Name").Value,
                        Description = d.Element("Description").Value,
                        SystemDefined = (bool)d.Element("SystemDefined"),
                    };

            return q.FirstOrDefault();
        }
        public int Save(SiteModel page)
        {
            
            var q = from d in doc.Descendants("Site")
                    select new SiteModel
                    {
                        Id = (int)d.Element("Id"),
                    };
           

            if (q.Count() > 0)
                page.Id = q.Max(s => s.Id) + 1;
            else
                page.Id = 1;


            var oMember = new XElement("Site",
             new XElement("Id", page.Id),
             new XElement("Name", page.Name),
             new XElement("Description", page.Description),
             new XElement("SystemDefined", page.SystemDefined)
             );

            doc.Root.Add(oMember);
            doc.Save(FilePath);
            return page.Id;
        }
        // Delete Record
        public void Delete(int id)
        {

            doc.Root.Elements("Site").Where(i => (int)i.Element("Id") == id).Remove();
            doc.Save(FilePath);
        }

        // Edit Record
        public void Edit(SiteModel page)
        {

            XElement node = doc.Root.Elements("Site").Where(i => (int)i.Element("Id") == page.Id).FirstOrDefault();
            node.SetElementValue("Name", page.Name);
            node.SetElementValue("Description", page.Description);
            node.SetElementValue("SystemDefined", page.SystemDefined);
            doc.Save(FilePath);
        }

        public void Dispose()
        {
            doc = null;
        }
    }
}
